package com.ddtech.business.practice.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.practice.entity.PracticeVrRecord;
import com.ddtech.business.practice.mapper.PracticeVrRecordMapper;
import com.ddtech.business.practice.query.PracticeVrRecordQuery;
import com.ddtech.business.practice.service.PracticeVrRecordService;
import com.ddtech.business.practice.service.PracticeVrRecordStudentService;
import com.ddtech.business.practice.util.SerialNumberGenerator;
import com.ddtech.business.practice.vo.PracticeVrRecordDetailsVo;
import com.ddtech.business.practice.vo.PracticeVrRecordReqVo;
import com.ddtech.business.practice.vo.PracticeVrRecordStudentVo;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @ClassName:PracticeVrRecordServiceImpl description：
 * @author:wei_zhang(xiao)
 * @date: 2024-07-16 22:41
 */
@Service
public class PracticeVrRecordServiceImpl  extends ServiceImpl<PracticeVrRecordMapper, PracticeVrRecord> implements PracticeVrRecordService {

    @Autowired
    private PracticeVrRecordStudentService practiceVrRecordStudentService;

    @Resource
    private PracticeVrRecordMapper practiceVrRecordMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gradeSave(PracticeVrRecordReqVo practiceVrRecordReqVo) {
        PracticeVrRecord practiceVrRecord = new PracticeVrRecord();
        practiceVrRecord.init();
        practiceVrRecord.setCourseId(practiceVrRecordReqVo.getCourseId());
        practiceVrRecord.setDanceType(practiceVrRecordReqVo.getDanceType());
        practiceVrRecord.setPracticeName(practiceVrRecordReqVo.getPracticeName());
        practiceVrRecord.setUploadUser(practiceVrRecordReqVo.getUploadUser());
        practiceVrRecord.setDvFile(practiceVrRecordReqVo.getDvFile());
        practiceVrRecord.setChartData(practiceVrRecordReqVo.getChartData());
        practiceVrRecord.setStandardDanceId(practiceVrRecordReqVo.getStandardDanceId());
        practiceVrRecord.setPracticeNumber(SerialNumberGenerator.generateSerialNumber());
        this.save(practiceVrRecord);
        practiceVrRecordStudentService.savePracticeVrRecordStudent(practiceVrRecord.getId(),practiceVrRecordReqVo.getStudentList());
    }

    @Override
    public Object pageList(PracticeVrRecordQuery practiceVrRecordQuery) {
        IPage<Map<String, Object>> page = practiceVrRecordMapper.pageList(practiceVrRecordQuery.populatePage(), practiceVrRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object practiceDetail(String id) {
        PracticeVrRecord practiceVrRecord = this.getById(id);
        PracticeVrRecordDetailsVo practiceVrRecordDetailsVo = new PracticeVrRecordDetailsVo();
        practiceVrRecordDetailsVo.setId(practiceVrRecord.getId());
        practiceVrRecordDetailsVo.setPracticeName(practiceVrRecord.getPracticeName());
        practiceVrRecordDetailsVo.setPracticeNumber(practiceVrRecord.getPracticeNumber());
        practiceVrRecordDetailsVo.setDvFile(practiceVrRecord.getDvFile());
        practiceVrRecordDetailsVo.setStandardDanceId(practiceVrRecord.getStandardDanceId());
        practiceVrRecordDetailsVo.setUploadUser(practiceVrRecord.getUploadUser());
        practiceVrRecordDetailsVo.setChartData(practiceVrRecord.getChartData());
        practiceVrRecordDetailsVo.setCreateTime(practiceVrRecord.getCreateTime());
        List<PracticeVrRecordStudentVo> list = practiceVrRecordStudentService.getList(id);
        practiceVrRecordDetailsVo.setStudentList(list);
        return practiceVrRecordDetailsVo;
    }

    @Override
    public Object pageListCode(PracticeVrRecordQuery practiceVrRecordQuery) {
       List<String> code= SecurityUtils.getRoleCode();
        IPage<Map<String, Object>> page;
       if (code.contains("STUDENT")){
           practiceVrRecordQuery.setAccount(SecurityUtils.getUsername());
           page = practiceVrRecordMapper.pageListCodeByAccount(practiceVrRecordQuery.populatePage(), practiceVrRecordQuery);
       }else{
           page = practiceVrRecordMapper.pageListCode(practiceVrRecordQuery.populatePage(), practiceVrRecordQuery);
       }

        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }
}
